// package-star/pages/brand-spokesman-audit/index.js
import { ManageTargetInfoUpdateMixins, PageScrollListMixins } from '../../../behaviors/page';
import { TARGET_TYPE } from '../../../constants/common';
import { showLoading, hideLoading } from '../../../common/env';
import { getSpokesmanRequestList, auditSpokesmanRequest } from '../../../api/brd';

Page({
    mixins: [ManageTargetInfoUpdateMixins, PageScrollListMixins],
    data: {
        TARGET_TYPE,
        tab: 'true',
        tabs: [
            { value: 'true', name: '待审核' },
            { value: 'false', name: '已审核' },
        ],
    },
    onLoad(options) {
    },
    async _handleGetList(pageNo) {
        !this.data.loaded && showLoading('加载中...');
        const { pageData, tab } = this.data;
        let { ts } = pageData;
        pageNo === 1 && (ts = Date.now());
        const { rows = [], total } = await getSpokesmanRequestList({ pageNo, ts, isWaitAudit: tab });
        this.setData({
            pageData: {
                ...pageData,
                refresh: false,
                ts,
                pageNo,
                total,
                rows: pageNo == 1 ? rows : [...pageData.rows, ...rows],
            },
        });
        if (!this.data.loaded) hideLoading(); this.setData({ loaded: true });
    },
    handleTabChange(e) {
        const { name } = e.detail;
        this.setData({ tab: name });
        this._handleGetList(1);
    },
    async handleAgree(e) {
        const reqId = e.detail;
        const flag = await auditSpokesmanRequest('approve', reqId);
        if (flag) this._handleGetList(1);
    },
    async handleDisagree(e) {
        const reqId = e.detail;
        const flag = await auditSpokesmanRequest('reject', reqId);
        if (flag) this._handleGetList(1);
    },
})
